package com.example.myapplication.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.example.myapplication.R;

/**
 * 在记录页面弹出时间对话框
 */
public class SelectTimeDialog extends Dialog implements View.OnClickListener {
    private EditText hourEt, minuteEt;
    private Button ensureBtn;
    private Button cancelBtn;
    private DatePicker dialogTimeDp;

    public interface  OnEnsureListener{
        public void onEnsure(String time,int year,int month,int day);
    }
    OnEnsureListener onEnsureListener;

    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    public SelectTimeDialog(@NonNull Context context) {
        super(context);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_time);
        initView();
        hideDatePickerHeader();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_time_btn_cancel:
                cancel();
                break;
            case R.id.dialog_time_btn_ensure:
                int year = dialogTimeDp.getYear();//选择年份
                int month = dialogTimeDp.getMonth()+1;//选择月份
                int dayOfMonth = dialogTimeDp.getDayOfMonth();//选择日期
                String monthStr="";
                if (month<10){
                    monthStr="0"+month;
                }else {
                    monthStr = String.valueOf(month);
                }
                String dayStr="";
                if (dayOfMonth<10){
                    dayStr="0"+dayOfMonth;
                }else {
                    dayStr = String.valueOf(dayOfMonth);
                }
                //获取输入的小时和分钟
                String hourStr = hourEt.getText().toString();
                String minuteStr = minuteEt.getText().toString();
                int hour=0;
                int minute=0;
                if (!TextUtils.isEmpty(hourStr)){
                    hour=Integer.parseInt(hourStr);
                    hour=hour%24;
                }
                if (!TextUtils.isEmpty(minuteStr)) {
                    minute=Integer.parseInt(minuteStr);
                    minute=minute%60;
                }
                hourStr=String.valueOf(hour);
                minuteStr=String.valueOf(minute);
                if(hour<10){
                    hourStr="0"+hour;
                }
                if(minute<10){
                    minuteStr="0"+minute;
                }
                Log.d("cccc", "month:"+month);
                String time = year+"年"+monthStr+"月"+dayStr+"日 "+hourStr+":"+minuteStr;
                onEnsureListener.onEnsure(time,year,month,dayOfMonth);
                cancel();
                break;
        }
    }

    private void initView() {
        hourEt = (EditText) findViewById(R.id.dialog_time_et_hour);
        minuteEt = (EditText) findViewById(R.id.dialog_time_et_minute);
        cancelBtn = (Button) findViewById(R.id.dialog_time_btn_cancel);
        ensureBtn = (Button) findViewById(R.id.dialog_time_btn_ensure);
        dialogTimeDp = (DatePicker) findViewById(R.id.dialog_time_dp);
        cancelBtn.setOnClickListener(this);
        ensureBtn.setOnClickListener(this);
    }
        //隐藏DatePicker头布局
    private void hideDatePickerHeader(){
        ViewGroup rootView = (ViewGroup) dialogTimeDp.getChildAt(0);
        if (rootView==null) {
            return;
        }
        View headerView = rootView.getChildAt(0);
        if (headerView==null) {
            return;
        }
        //5.0+
        //获取到系统DatePicker头布局
        int headerId = getContext().getResources().getIdentifier("day_picker_selector_layout", "id", "android");
        if (headerId==headerView.getId()) {
            headerView.setVisibility(View.GONE);
            //获取根布局参数
            ViewGroup.LayoutParams layoutParams = rootView.getLayoutParams();
            layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            rootView.setLayoutParams(layoutParams);
            ViewGroup animator = (ViewGroup) rootView.getChildAt(1);
            ViewGroup.LayoutParams layoutParamsAnimator = animator.getLayoutParams();
            layoutParamsAnimator.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            animator.setLayoutParams(layoutParamsAnimator);

            View child = animator.getChildAt(0);
            ViewGroup.LayoutParams layoutParamsChild = child.getLayoutParams();
            layoutParamsChild.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            child.setLayoutParams(layoutParamsChild );
            return;
        }
        //6.0+
        headerId = getContext().getResources().getIdentifier("date_picker_header", "id", "android");
        if (headerId == headerView.getId()) {
            headerView.setVisibility(View.GONE);
        }
    }
}
